<?php
namespace app\admin\model;
use think\Model;
use think\model\concern\SoftDelete;

class Goods extends Model
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';

    public function searchNameAttr($query, $value, $data)
    {
        $query->where('g.name','like', "%{$value}%");
    }
    public function searchIsShowAttr($query, $value, $data)
    {
        $query->where('is_show','=', $value);
    }
    public function searchIsHotAttr($query, $value, $data)
    {
        $query->where('is_hot','=', $value);
    }
    public function searchIsDownAttr($query, $value, $data)
    {
        $query->where('is_down','=', $value);
    }

    public function getAddtimeAttr($value)
    {
        return date('Y-m-d',$value);
    }

    //查询goods的全部数据
    public static function goodlist($where,$order)
    {
        $list=Goods::alias('g')
            ->withSearch(array_keys($where),$where)
            ->order($order)
            ->leftjoin('category c','g.cid = c.id')
            ->field('g.id,g.name,g.price,g.photo_x,is_show,is_hot,is_down,g.cid,c.name as cname,g.addtime')
            ->paginate(['list_rows'=>5,'query'=>$where]);
        return $list;
    }
    //修改字段
    public static function xiuGoodlist($id)
    {
        //渲染商品修改
        $list=Goods::alias('g')
            ->leftjoin('category c','g.cid = c.id')
            ->field('g.id,g.name,g.price,g.price_shop,g.num,g.company,g.renqi,g.pro_number,g.content,g.sort,g.photo_x,is_show,is_hot,is_down,g.cid,c.name as cname,g.addtime')
            ->find($id)->toArray();
        return $list;
    }
    //商品页的添加
    public static function goodAdd($arr)
    {
        $list=Goods::create($arr);
        return $list;
    }
    //查询good删除的全部数据
    public static function goodDel($where,$order)
    {
        $list=Goods::onlyTrashed()
            ->alias('g')
            ->withSearch(array_keys($where),$where)
            ->order($order)
            ->leftjoin('category c','g.cid = c.id')
            ->field('g.id,g.name,g.price,g.photo_x,is_show,is_hot,is_down,g.cid,c.name as cname,g.addtime')
            ->paginate(['list_rows'=>5,'query'=>$where]);
        return $list;
    }
    //真删
    public static function zhenGoodDel($shanId)
    {
        $goods=Goods::find($shanId);
        $goods=$goods->force()->delete();
        return  $goods;
    }
    //恢复删除的数据
    public static function huiGoodDel($huiId)
    {
        $goods=Goods::onlyTrashed()->find($huiId);
        $goods=$goods->restore();
        return $goods;
    }
    //软删除
    public static function goodListdel($delId)
    {
        $goods=Goods::find($delId);
        $goods=$goods->delete();
        return $goods;
    }
}